fix(types): propagate destination generic to ConversionResult return#95
Open
ryanyue123 wants to merge 1 commit into
Open
fix(types): propagate destination generic to ConversionResult return#95ryanyue123 wants to merge 1 commit into
ryanyue123 wants to merge 1 commit into
Conversation
Both OfficeParserAST.to() and OfficeConverter.convert() declared their
return as Promise<ConversionResult> (defaulting D to the full
UniversalGeneratorFormat union) instead of Promise<ConversionResult<D>>.
Callers passing a literal destination like "md" or "pdf" couldn't narrow
.value from the union (string | Uint8Array | OfficeChunk[]) to the
specific type the runtime conditional guarantees.
- OfficeParserAST.to (types.ts + utils/astUtils.ts impl):
Promise<ConversionResult> → Promise<ConversionResult<D>>.
- OfficeConverter.convert: add an explicit D generic (defaults to
SupportedDestination<T>) so destination's literal flows into the
ConversionResult and OfficeConverterConfig type args.
Now:
await OfficeConverter.convert(buf, "md") → value: string
await OfficeConverter.convert(buf, "pdf") → value: Uint8Array
await ast.to("chunks") → value: OfficeChunk[]
No runtime change; existing call sites that read value directly were
already relying on this narrowing implicitly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OfficeParserAST.to()andOfficeConverter.convert()both declare their return asPromise<ConversionResult>(soDdefaults to the fullUniversalGeneratorFormatunion) instead ofPromise<ConversionResult<D>>. The result: callers passing a literal destination like"md"or"pdf"can't narrow.valuefrom the full union (string | Uint8Array | OfficeChunk[]) to the specific type the runtime conditional already guarantees.This PR threads the destination generic into the return type so the conditional in
ConversionResult.valueactually flows from the destination argument.Changes
types.ts:OfficeParserAST.to<D>(...): Promise<ConversionResult>→Promise<ConversionResult<D>>.utils/astUtils.ts: matching implementation return type.OfficeConverter.ts: add an explicitD extends SupportedDestination<T>generic so the destination literal carries through toConversionResult<D>andOfficeConverterConfig<D, T>.No runtime behavior change — the conditional in
ConversionResult.valuealready produced the right runtime type per destination; this just lets TypeScript see it.Verification
Build (
npm run build:node) passes locally.Motivation
Hit this in a production worker where we extract markdown from uploaded proposals. The workaround we ship today is a runtime
typeof value !== "string"guard purely to satisfy the type system; the guard can never fire at runtime because the library's own conditional type guaranteesstringfor"md". Once this PR ships we drop that guard and let TypeScript reflect the runtime contract.